import { getProducts } from 'api/products'
export default {
  namespaced: true,
  state: {
    productsList: []
  },
  mutations: {
    setProductsList (state, payload) {
      state.productsList = payload.productsList
    },
    // 对商品库存信息进行操作
    reduceProductInventory (state, payload) {
      const productItem = state.productsList.find(
        (val) => val.id === payload.id
      )
      productItem.inventory--
    }
  },
  actions: {
    // 得到商品列表
    async getProductsList ({ commit }) {
      const res = await getProducts()
      commit('setProductsList', {
        productsList: res
      })
    }
  }
}
